package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginAppUserContext;
import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.service.UserInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {


    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private RoomLabelMapper roomLabelMapper;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;

    @Autowired
    private RoomFacilityMapper roomFacilityMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> listByPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        IPage<RoomItemVo> list = baseMapper.selectListByPage(page, queryVo);
        return list;
    }

    @Override
    public RoomDetailVo listRoomDetailVoAppBy(Long id) {
        // 获取房间基本信息
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = baseMapper.selectById(id);
        // 获取最小租金
        BigDecimal minRent = baseMapper.selectMinRent(roomInfo.getApartmentId());
        BeanUtils.copyProperties(roomInfo, roomDetailVo);



        // 获取 apartmentItemVo 基本信息
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setMinRent(minRent);



        // 获取 graphVoList 列表信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        List<GraphInfo> graphInfoList = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = graphInfoList.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).collect(Collectors.toList());


        // 获取 labelInfoList 列表信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListById(id);
        // 封装 ApartmentItemVo 信息
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);

        // 获取 attrValueVoList 列表信息
         List<AttrValueVo> attrValueVoList = roomAttrValueMapper.selectListByRoomId(id);
        // 获取 facilityInfoList 列表信息
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectListByRoomId(id);
        // 获取 feeValueVoList 列表信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(roomInfo.getApartmentId());
        // 获取 paymentTypeList 列表信息
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        // 获取 leaseTermList 列表信息
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
        // 查询房间是否已入住
        LambdaQueryWrapper<LeaseAgreement> leaseInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseInfoLambdaQueryWrapper.eq(LeaseAgreement::getRoomId, id);
        // status in 2,5,7 表示已签约 == 已入住
        leaseInfoLambdaQueryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.RENEWING,LeaseStatus.WITHDRAWING);
        Long count = leaseAgreementMapper.selectCount(leaseInfoLambdaQueryWrapper);


        // 封装 RoomDetailVo 信息
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        // count > 0 表示房间已入住
        roomDetailVo.setIsCheckIn(count > 0);

        // 保存用户浏览记录
        UserInfo userInfo = userInfoService.getUserIdByPhone(LoginAppUserContext.getLoginAppUser().getPhone());
        browsingHistoryService.saveHistory(userInfo.getId(), id);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
        IPage<RoomItemVo> list = baseMapper.pageItemByApartmentId(page, id);
        return list;
    }


}




